
// 定义时间戳格式化函数
 export const formatTimestamp = (timestamp) => {
  const date = new Date(timestamp);
  
  const year = date.getFullYear();
  const month = String(date.getMonth() + 1).padStart(2, '0'); // 0-11 → 1-12
  const day = String(date.getDate()).padStart(2, '0');
  const hours = String(date.getHours()).padStart(2, '0');
  const minutes = String(date.getMinutes()).padStart(2, '0');
  const seconds = String(date.getSeconds()).padStart(2, '0');
  
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

// 定义转化单位函数
export function formatMoney(value, withYuan = true) {
  const num = parseFloat(value);
  if (isNaN(num)) return withYuan ? '0元' : '0';
  
  if (num >= 100000000) {
    return (num / 100000000).toFixed(2) + '亿元';
  } else if (num >= 10000) {
    return (num / 10000).toFixed(2) + '万元';
  } else {
    return num.toFixed(2) + (withYuan ? '元' : '');
  }
}

// 带千分位分隔符的版本
export function formatMoneyWithComma(value, withYuan = true) {
  const num = parseFloat(value);
  if (isNaN(num)) return withYuan ? '0元' : '0';
  
  let unit = '';
  let formattedNum = 0;
  
  if (num >= 100000000) {
    formattedNum = num / 100000000;
    unit = '亿';
  } else if (num >= 10000) {
    formattedNum = num / 10000;
    unit = '万';
  } else {
    formattedNum = num;
  }
  
  // 添加千分位分隔符
  const parts = formattedNum.toFixed(2).split('.');
  
  return parts.join('.') + unit;
}
  
// 兼容性复制函数

export function copyTextToClipboard(text) {
  return new Promise((resolve, reject) => {
    // 使用临时 textarea 元素
    const textArea = document.createElement('textarea');
    textArea.value = text;
    textArea.style.top = '0';
    textArea.style.left = '0';
    textArea.style.position = 'fixed';
    textArea.style.opacity = '0';
    document.body.appendChild(textArea);

    textArea.focus();
    textArea.select();

    try {
      const successful = document.execCommand('copy');
      document.body.removeChild(textArea);
      if (successful) {
        resolve();
      } else {
        reject(new Error('execCommand failed'));
      }
    } catch (err) {
      document.body.removeChild(textArea);
      reject(err);
    }
  });
}


// 提示
export const toast = (message, duration = 2000) => {
  uni.showToast({
    title: message,
    duration: duration,
    icon: 'none',
  });
};


 // 生成随机颜色 
 export function getRandomRGBColor() {
   // 生成150-255之间的随机数，确保颜色是浅色
  const r = Math.floor(Math.random() * 106) + 150; // 150-255
  const g = Math.floor(Math.random() * 106) + 150; // 150-255
  const b = Math.floor(Math.random() * 106) + 150; // 150-255
  return `rgba(${r}, ${g}, ${b}, 0.5)`; // 保持50%透明度
 }

// 小数部分的样式
export const formatBalance = (amount) => {
  if (!amount && amount !== 0) return { integer: '0', decimal: '.00' };
  
  const amountNum = parseFloat(amount);
  if (isNaN(amountNum)) return { integer: '0', decimal: '.00' };
  
  // 转为字符串处理
  const amountStr = amountNum.toString();
  
  // 判断是否包含小数点
  if (amountStr.includes('.')) {
    const parts = amountStr.split('.');
    return {
      integer: parts[0],
      decimal: '.' + parts[1]
    };
  } else {
    // 整数情况，添加 .00
    return {
      integer: amountStr,
      decimal: '.00'
    };
  }
};

// 产品名称格式化函数 - 限制长度为6个字符，超出部分用省略号代替
export const formatProductName = (name, maxLength = 6) => {
  if (!name) return '';
  
  // 转换为字符串
  const nameStr = String(name);
  
  // 如果长度小于等于最大长度，直接返回
  if (nameStr.length <= maxLength) {
    return nameStr;
  }
  
  // 截取前6个字符并添加省略号
  return nameStr.substring(0, maxLength) + '...';
};